<!--  -->

<script type="text/javascript">
$(document).ready(function(){

//	$("#flex_DataGrid").flexigrid
//			(
//			{
//			url: "<?php echo base_url()?>user/userList/readDataFromDB",
//			dataType: 'json',
//			colModel : [
//				{display: 'Check', name : '', width : 40, sortable : false, align: 'center',checkbox:true},
//				{display: 'ID', name : 'id', width : 100, sortable : true, align: 'center'},
//				{display: 'SerialNumber', name : 'serialNumber', width : 100, sortable : true, align: 'center'},
//				{display: 'Type', name : 'type', width : 100, sortable : true, align: 'left'},
//				{display: 'test', name : 'type22', width : 100, sortable : true, align: 'left'}
//				],
//			buttons : [
//				{name: '选择:'},
//				{name: '全选', 	onpress : btnSelectFunofFlexi},
//				{name: '无', 	onpress : btnSelectFunofFlexi},
//				{name: '反选', 	onpress : btnSelectFunofFlexi},
//				{name: '前一半', 	onpress : btnSelectFunofFlexi},
//				{name: '区间', 	onpress : btnSelectFunofFlexi},
//				{separator: true},
//				{name: 'Add', bclass: 'add', onpress : deleteRowsFromFlexi},
//				{name: 'Delete', bclass: 'delete', onpress : deleteRowsFromFlexi},
//				{separator: true},
//				{name: '标为接受'},
//				{name: '标为等待'},
//				{name: '标为拒绝'},
//				{separator: true}
//
//				],
//			searchitems : [
//				{display: 'ID', name : 'id', isdefault: true}
////				{display: 'Name', name : 'name', isdefault: false},
////				{display: 'ISO', name : 'iso', isdefault: false}
//				],
//			sortname: "id",
//			sortorder: "desc",		// asc 
//			usepager: true,
//			title: 'Order',			// 表格名称
//			useRp: true,
//			rp: 100,					// 每页多少条记录
//			showTableToggleBtn: true,
//			width: 680,					//表格宽度
//			height: 455					//表格高度
//			}
//			);   
	
	 //--------------------------
	 // 选择某个表时,获取其所有的字段名称 
	 $("#m_selectTable").click(function(){
		 var tb = document.getElementById("m_selectTable").options[document.getElementById("m_selectTable").options.selectedIndex].value;

	 	 $.ajax({
	         url:"<?php echo base_url()?>user/userList/getAllColName",
	         type: 'POST',         //数据发送方式
	         dataType: 'json',     //接受数据格式
	         data:{"table":tb}, 
	         success:function(data){
	        	 var fieldsNames = eval(data);
				 //alert(fieldsNames.length);
				 
			     createFlexigrid(fieldsNames,tb);
			     
		     }, //回传函数(这里是函数名)
	     	 error:function(){
			     	//alert('ERROR');
			 }
	       }); // 返回了属性列表 

	       
	 	 });


});


// 创建Flexigrid表格 
function createFlexigrid(cols,tb){
	
	{
		var colModelArrary = new Array();
		colModelArrary[0] = {display: 'Check', name : 'check', width : 40, sortable : false, align: 'center', checkbox:true};
		for (i = 0; i < cols.length; i++){
			colModelArrary[i+1] = {display: cols[i], name : cols[i], width : 100, sortable : false, align: 'left'};
		}
		colModelArrary[1].sortable = true;
	}
	{
		var searchCols = new Array();
		searchCols[0] = {display: cols[0], name : cols[0], isdefault: true};
		for (i = 1; i < cols.length; i++){
			searchCols[i] = {display: cols[i], name : cols[i], isdefault: false};
		}
		
	}

	var timeStartO =  new Date();
	var timeEndO =  new Date();
	timeStartO.setFullYear(1980);
	var timeStart = timeStartO.format("yyyy-MM-dd hh:mm:ss");
	var timeEnd = timeEndO.format("yyyy-MM-dd hh:mm:ss");
 
	var flexigrid_p = {// 表格控件的参数 
			url: "<?php echo base_url()?>user/userList/readDataFromDB",
			dataType: 'json',
			colModel : colModelArrary,
			buttons : [
				{name: '选择:'},
				{name: '全选', 	onpress : btnSelectFunofFlexi},
				{name: '无', 	onpress : btnSelectFunofFlexi},
				{name: '反选', 	onpress : btnSelectFunofFlexi},
				{name: '前一半', 	onpress : btnSelectFunofFlexi},
				{name: '区间', 	onpress : btnSelectFunofFlexi},
				{separator: true},
				{name: 'Add', bclass: 'add', onpress : deleteRowsFromFlexi},
				{name: 'Delete', bclass: 'delete', onpress : deleteRowsFromFlexi},
				{separator: true}
				],
			searchitems : searchCols,
			sortname: "id",
			sortorder: "desc",		// asc 
			usepager: true,
			title: tb,			// 表格名称
			useRp: true,
			rp: 40,					// 每页多少条记录
			showTableToggleBtn: true,
			width: 1000,					//表格宽度
			height: 455	,				//表格高度
			sqlTable : tb,
			timeFlag : 0,	//是否有时间筛选
			tStart: timeStart,
			tEnd: timeEnd,
			orderStatus : "null",
			orderStatusFlag : 0
			};

	// 创建表格控件 
	$("#flex_DataGrid").remove();
	$("#div_flex_DataGrid").html("");
	$("#div_flex_DataGrid").prepend("<table id='flex_DataGrid' style='display:none' title='notload'></table>");
	$("#flex_DataGrid").flexigrid(flexigrid_p);
 
//	if(document.getElementById("flex_DataGrid").title == "notload")
//	{
//		$("#flex_DataGrid").remove();
//		$("#div_flex_DataGrid").prepend("<table id='flex_DataGrid' style='display:none' title='notload'></table>");
//		$("#flex_DataGrid").flexigrid(flexigrid_p);
//	}
//	else{
//		//$("#flex_DataGrid").flexReload(flexigrid_p);
//		}
//			   
	

}

function btnSelectFunofFlexi(com,grid)
{
	//alert(com);
	var btnID = "";
	grid="#flex_DataGrid";
	switch(com)
	{
	case "全选":
		btnID = "btSelectAll";
		break;
	case "无":
		btnID = "btSelectNone";
		break;
	case "反选":
		btnID = "btSelectInverse";
		break;
	case "前一半":
		btnID = "btSelectFirstHalf";
		break;
	case "区间":
		btnID = "btSelectSection";
		break;
	}
	
	if(btnID=="")
		return false;
	$("#"+btnID).trigger("click");

	

}

// 设置标题栏的状态信息 
function setTitleofFlexi(name,grid){
	if(!name)
		return false;
	if(!grid){ grid="#flex_DataGrid";}
	$('.ftitle',grid.mDiv).html(name);
	
}

function deleteRowsFromFlexi(com,grid)
{
	var sqlTableName="";
	sqlTableName = document.getElementById("m_selectTable").options[document.getElementById("m_selectTable").options.selectedIndex].value;
	if(sqlTableName=="") return;
    if (com=='Delete')
        {
    	   checkAllLines($("#flex_DataGrid"));	
           if($('.trSelected',grid).length>0){
		   if(confirm('操作提醒: Delete ' + $('.trSelected',grid).length + ' items?')){
            var items = $('.trSelected',grid);
            var itemlist ='';
        	for(i=0;i<items.length;i++){
				itemlist+= items[i].id.substr(3)+",";
			}

        	var param = [
					 { name : 'items', value :itemlist },
					 { name : 'tableName', value : sqlTableName }
					 ];
 
			$.ajax({
			   type: "POST",
			   dataType: "json",
			   url: "<?php echo base_url()?>user/userList/delRows",
			   data: param,
			   success: function(data){
			   	//alert("DELETE: "+data.query+" - Total affected rows: "+data.total);
			   $("#flex_DataGrid").flexReload();
			   }
			 });
			}
			} else {
				alert("没有要删除的记录");
				return false;
			} 
        }
    else if (com=='Add')
        {
            alert('Add New Item Action');
           
        }            
}

function sortAlpha(com)
{ 
	jQuery('#flex_DataGrid').flexOptions({newp:1, params:[{name:'letter_pressed', value: com},{name:'qtype',value:$('select[name=qtype]').val()}]});
	jQuery("#flex_DataGrid").flexReload(); 
}


//Flexigrid 中checkbox选中的行设为选中状态trSelected
function checkAllLines(grid){
	 
	$('tbody tr',grid.bDiv).each(function(){
		//$(this).toggleClass('trSelected');
		$(this).removeClass('trSelected');
		});

	$('tbody tr',grid.bDiv).each(function(){

		if($('input',$(this).children()).attr("name") == "SelectBoxInFlexiGrid"){
	  		if($('input',$(this).children()).attr("checked")){
	  			$(this).addClass('trSelected');
	    	}
		}
	});

	//alert(grid.trSelectNums);
	var checkNum=$("input[name='SelectBoxInFlexiGrid']:checked").length;

	
}

</script>

<div id="value" style="margin:0 10px; clear:both;"></div>
<div id="div_flex_DataGrid">
<!--  -->
<table id="flex_DataGrid" style="display:none" title="notload"></table>
</div>
<div id="value" style="margin:0 10px; clear:both;"></div>
<br /><br />
 



